using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using CodePlex.RoundedButton.Code.JobDefinitions;

namespace CodePlex.RoundedButton.Features.BrowserDefinitionTimerJob
{
	/// <summary>
	/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
	/// </summary>
	/// <remarks>
	/// The GUID attached to this class may be used during packaging and should not be modified.
	/// </remarks>

	[Guid("030f4d2d-6ffb-4169-afd9-38115a544494")]
	public class BrowserDefinitionTimerJobEventReceiver : SPFeatureReceiver
	{
		public const string jobName = "browser-definition-timer-job";

		public override void FeatureActivated(SPFeatureReceiverProperties properties)
		{
			SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
			BrowserJobDefinition newJob = new BrowserJobDefinition(jobName, webApp)
			{
				Schedule = new SPOneTimeSchedule(DateTime.Now)
			};
			newJob.Properties.Add("add", true);
			newJob.Update();
		}

		public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
		{
			SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
			BrowserJobDefinition newJob = new BrowserJobDefinition(jobName, webApp)
			{
				Schedule = new SPOneTimeSchedule(DateTime.Now)
			};
			newJob.Properties.Add("add", false);
			newJob.Update();
		}
	}
}
